﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using Cinchoo.Core.IO;
using System.ComponentModel;
using System.IO;
using System.Reflection;
using System.Xml;

namespace Cinchoo.Core.ETL
{
    [XmlRoot("sqliteCSVFileTableImportTask")]
    public class ChoETLSQLiteCSVFileTableImportTaskDef : ChoETLTaskDef
    {
        [ChoETLTaskDefPropertyInfo("CONNECTION_STRING", IsRequired = true)]
        [XmlAttribute("connectionString")]
        public string ConnectionString { get; set; }

        [ChoETLTaskDefPropertyInfo("TRNSACTION_SCOPE")]
        [XmlAttribute("transactionScope")]
        [DefaultValue(true)]
        public bool TransactionScope{ get; set; }

        [ChoETLTaskDefPropertyInfo("TABLE_NAME", IsRequired = true)]
        [XmlAttribute("tableName")]
        public string TableName{ get; set; }

        [ChoETLTaskDefPropertyInfo("EMPTY_TABLE")]
        [XmlAttribute("emptyTable")]
        [DefaultValue(true)]
        public bool EmptyTable { get; set; }

        [ChoETLTaskDefPropertyInfo("FILE_PATH", IsRequired = true)]
        [XmlAttribute("filePath")]
        public string FilePath { get; set; }

        [ChoETLTaskDefPropertyInfo("SEPERATOR")]
        [XmlAttribute("seperator")]
        [DefaultValue(",")]
        public string Seperator { get; set; }

        protected override ChoETLTask Construct(ChoETLWorkflow workflow)
        {
            ChoGuard.ArgumentNotNullOrEmpty(ConnectionString, "ConnectionString");
            ChoGuard.ArgumentNotNullOrEmpty(FilePath, "FilePath");

            string importRecSql = String.Empty;
            if (Seperator.IsNullOrWhiteSpace())
                Seperator = ",";

            if (!TableName.IsNullOrWhiteSpace())
            {
                using (StreamReader sr = File.OpenText(FilePath))
                {
                    string line = sr.ReadLine();
                    if (!line.IsNullOrWhiteSpace())
                    {
                        int recCount = line.Split(Seperator).Length;
                        importRecSql = "INSERT INTO {0} VALUES (".FormatString(TableName);
                        for (int i = 0; i < recCount; i++)
                            importRecSql += "{0} @{1}".FormatString(i == 0 ? String.Empty : ",", i);
                        importRecSql += ")";
                    }
                    else
                        return ChoETLNullTask.Instance;
                }
            }
            else
                throw new ArgumentNullException("Missing TABLE_NAME.");

            ChoETLTask task = new ChoETLSQLiteConnectionJob(ConnectionString, TransactionScope);
            if (EmptyTable)
                task = task.ContinueWith(new ChoETLSQLiteCommandJob(ConnectionString, "DELETE FROM {0}".FormatString(TableName)));
            task = task.ContinueWith(new ChoETLCSVFileLoaderTask(FilePath, Seperator));
            task = task.ContinueWith(new ChoETLSQLiteCommandTask(ConnectionString, importRecSql));

            return task; 
        }
    }
}
